window.onload = function () {
    document.getElementById("my-mine").className = "add_class";
    document.getElementById("personage").className = "add-nav-active";
    station_list();
};


// 加载行程列表
function station_list(sent_str="", page=1) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/personal-center/my_mine.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page)+"&search=ture");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
            }
        }
    };
}


function create_table(count_num, json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
        td.innerHTML = i+1;td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["schedule_name"];td1.title = json_data[i]["schedule_name"];td1.style.paddingLeft = "1%";
        td2.innerHTML = json_data[i]["schedule_adress"];td2.title = json_data[i]["schedule_adress"];td2.style.paddingLeft = "1%";
        td3.innerHTML = json_data[i]["start_time"].replace("T", " ").split(".")[0];td3.title = json_data[i]["start_time"].replace("T", " ").split(".")[0];td3.style.paddingLeft = "1%";
        td4.innerHTML = json_data[i]["end_time"].replace("T", " ").split(".")[0];td4.title = json_data[i]["end_time"].replace("T", " ").split(".")[0];td4.style.paddingLeft = "1%";
        td5.innerHTML = json_data[i]["allday"];td5.title = json_data[i]["allday"];td5.style.paddingLeft = "1%";
        td6.innerHTML = "<a href='#' onclick='Confirm_delete2("+json_data[i]["id"]+")' data-toggle=\"modal\" data-target=\".bd-example-modal-sm\">删除</a>";td6.style.textAlign="center";
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
        t_table.appendChild(tr);
    }
}


// 删除行程
function del_schedule(element_id) {
    document.getElementById("close-model2").click();
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/personal-center/my_mine.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(1)+"&delId="+String(element_id));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
            }
        }
    };
}


// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}


// 分页
function pagenum(dataTotals=0, page=1) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            station_list(sent_str='', page);
            document.getElementById("check_all").checked = false;
        }
    });
}




function chose_category(element_id) {
    if (element_id === "my_schedule") {
        document.getElementById(element_id).className = "button-but add-style";
        document.getElementById("add_schedule").className = "button-but";
        document.getElementById("select_tag").style.display = "none";
        document.getElementById("menu-navi").style.display = "none";
        document.getElementById("calendar").style.display = "none";
        document.getElementById("renderRange").style.display = "none";
        document.getElementById("schedule-table").style.display = "inline-block";
    }else {
        document.getElementById(element_id).className = "button-but add-style";
        document.getElementById("my_schedule").className = "button-but";
        document.getElementById("select_tag").style.display = "inline-block";
        document.getElementById("menu-navi").style.display = "inline-block";
        document.getElementById("calendar").style.display = "block";
        document.getElementById("renderRange").style.display = "inline-block";
        document.getElementById("schedule-table").style.display = "none";
    }
}



// 确认删除
function Confirm_delete2(ele_id) {
    document.getElementById("countersign").setAttribute("onclick", "del_schedule("+ele_id+")");
}
